from rest_framework.generics import ListAPIView, UpdateAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKUImage, SKU, SPU, SpecificationOption, SPUSpecification, Brand, GoodsChannel, \
    SKUSpecification, GoodsCategory, GoodsChannelGroup
from apps.meiduo_admin.serializers.imageserializer import ImageSerialzier, SpuSerializer, \
    SkuSSerialzier, CategorySerializer, BrandSerializer, CategoriesSerializer, \
    SPUSerializer, SPUSpecsSerializer2, SKUModerSerializer, SpecsModelSerializer, SpecsModelSerializer2, \
    BrandModelSerializer, GoodsChannelModelSerializer, GoodsChannelGroupModelSerializer, OrderinfoSerializer, \
    OrderGoodsSerializer, OrderinfoStatusSerializer
from apps.meiduo_admin.serializers.search_filter import Seacher
from apps.orders.models import OrderInfo, OrderGoods
from utils.upload_image import upload



# 获取图片以及上传图片到七牛云上面
class ImageModelView(ModelViewSet):
    serializer_class = ImageSerialzier
    queryset = SKUImage.objects.all().order_by('id')
    def create(self,request,*args,**kwargs):
        data = request.data.get('image')
        ret, info = upload(data)
        if info.status_code != 200:
            return Response('上传失败',status=201)
        sku_id = request.data.get('sku')
        img = SKUImage.objects.create(sku_id=sku_id,image=ret.get('key'))
        dict1 = {
            'id':img.id,
            'sku':sku_id,
            'image':img.image.url
        }
        return Response(dict1,status=201)
    def update(self,request,*args,**kwargs):
        data = request.data.get('image')
        ret, info = upload(data)
        image_url = ret.get('key')
        sku_id = request.data.get('sku')
        img = SKUImage.objects.get(id=kwargs.get('pk'))
        img.image = image_url
        img.save()
        dict1 = {
            'id':img.id,
            'sku':sku_id,
            'image':img.image.url
        }
        return Response(dict1,status=201)


# 获取sku商品数据，，，进行添加数据
class SkuAllView(ListAPIView):
    queryset =  SKU.objects.all()
    serializer_class =  SkuSSerialzier
    pagination_class = None



# sku的获取数据和搜索以及增删改查
class SkuView(ModelViewSet):
    queryset =  SKU.objects.all().order_by('id')
    serializer_class =  SKUModerSerializer
    filter_backends = [Seacher]
    search_fields = ['id','name','price']







# 获取分类的数据
class CategoryView(ListAPIView):
    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = CategorySerializer
    pagination_class = None
# spu数据的获取
class SPUListAPIView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SpuSerializer
    pagination_class = None
# 获取spu商品规格信息
class SPUSpecsView(ListAPIView):
    serializer_class = SPUSpecsSerializer2
    pagination_class = None
    def get_queryset(self):
        pk = self.kwargs['pk']
        return SPUSpecification.objects.filter(spu_id=pk)





# 这是获取品牌的视图
class BrandView(ListAPIView):
    queryset = Brand.objects.all()
    pagination_class = None
    serializer_class = BrandSerializer

# 获取一级二级三级分类的数据
class CategoriesView(ListAPIView):
    queryset = GoodsCategory.objects.filter(parent_id=None)
    serializer_class = CategoriesSerializer
    pagination_class = None
# 这是获取商品的全部数据，，，三级分类
class CategoriesView2(ListAPIView):
    serializer_class = CategoriesSerializer
    pagination_class = None
    def get_queryset(self):
        pk = self.kwargs['pk']
        q = GoodsCategory.objects.filter(parent_id=pk)
        return q
# 这是spu的增删改查
class SPUView(ModelViewSet):
    queryset = SPU.objects.all().order_by('id')
    serializer_class = SPUSerializer






# 这是关于规格管理的视图，，，增删改查
class SpecsModelViewSet(ModelViewSet):
    queryset = SPUSpecification.objects.all().order_by('id')
    serializer_class =  SpecsModelSerializer
# 这是规格选项的管理，    增删改查
class SpecsModelViewSet2(ModelViewSet):
    queryset = SpecificationOption.objects.all().order_by('id')
    serializer_class = SpecsModelSerializer2
class SpecListAPIView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = SpecsModelSerializer
    pagination_class = None

# 这是关于频道管理的视图，，，增删改查
class GoodsChannelModelViewSet(ModelViewSet):
    queryset = GoodsChannel.objects.all().order_by('id')
    serializer_class = GoodsChannelModelSerializer
class CategoriesListAPIView(ListAPIView):
    queryset = GoodsChannelGroup.objects.all()
    serializer_class = GoodsChannelGroupModelSerializer
    pagination_class = None
class CategoriesListAPIView2(ListAPIView):
    queryset = GoodsCategory.objects.filter(parent_id=None)
    serializer_class = CategorySerializer
    pagination_class = None




#这是关于品牌管理的视图，，，增删改查
class BrandModelViewSet(ModelViewSet):
    queryset = Brand.objects.all().order_by('id')
    serializer_class = BrandModelSerializer
    def create(self,request,*args,**kwargs):
        data = request.data
        logo = data.get('logo')
        name = data.get('name')
        first_letter = data.get('first_letter')
        if not all([logo,name,first_letter]):
            return Response('失败',status=400)
        if len(first_letter) < 1:
            return Response('失败',status=400)
        ret ,info = upload(logo)
        if info.status_code != 200:
            return Response('上传失败',status=400)
        logo2 = ret.get('key')
        obj = Brand.objects.create(name=name,first_letter=first_letter,logo=logo2)
        dict1 = {
            'id': obj.id,
            'name': obj.name,
            'logo': obj.logo.url,
            'first_letter': obj.first_letter}
        return Response (dict1,status=201)
    def update(self,request,*args,**kwargs):
        # 接受前端传来的参数
        data = request.data
        logo = data.get('logo' )
        name = data.get('name')
        first_letter = data.get('first_letter')

        if not all([logo,name,first_letter]):
            return Response('失败',status=400)
        if len(first_letter) > 1:
            return Response('失败',status=400)
        ret, info = upload(logo)
        if info.status_code != 200:
            return Response('上传失败',status=400)
        logo2 = ret.get('key')
        obj= Brand.objects.get(id=kwargs.get('pk'))
        obj.name= name
        obj.first_letter = first_letter
        obj.logo = logo2
        obj.save()
        dict1 = {
            'id': obj.id,
            'name':name,
            'logo': obj.logo.url,
            'first_letter': first_letter
        }
        return Response(dict1,status=201)

class OrderinfoView(ModelViewSet):
    queryset = OrderInfo.objects.all().order_by('order_id')
    serializer_class =OrderinfoSerializer
    filter_backends = [Seacher]
    search_fields = ['order_id','create_time']
class StatusView(UpdateAPIView):
    queryset = OrderInfo.objects.all()
    serializer_class =OrderinfoStatusSerializer
# class OrderGoodsView(ModelViewSet):
#     queryset = OrderGoods
#     serializer_class = OrderGoodsSerializer

